package com.example.demo.controller;


import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.*;
import com.example.demo.service.CServiceService;
import com.example.demo.service.UserService;
import com.example.demo.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/cservice")
public class CServiceController {
    @Autowired
    private CServiceService cServiceService;
    @Autowired
    private UserService userService;


    public boolean Authentication() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String phonenumber = (String) map.get("phonenumber");
        User u = userService.findByphoneNumber(phonenumber);
     //   if (u.getUsertype() != 1) {
        if(u.getUserstatus()!=0){
            return false;
        }
        return true;
    }



    @PostMapping("/add")
    public Result add(@RequestBody @Validated CService cService) {
        if(!Authentication()){
            return Result.error("权限不足");
        }
        UUID uuid = UUID.randomUUID();
        String strUUID = uuid.toString().replaceAll("-","");
        cService.setSessionid(strUUID);
        cServiceService.add(cService);
        return Result.success(strUUID);
    }

//    @GetMapping("/list")
//    public Result<List<Announcement>> list() {
//        return Result.success(announcementService.findAll());
//    }

    @GetMapping("/list")
    public Result<PageBean<CService>> list(
            int pageNum,
            int pageSize,
            @RequestParam(required = false) Integer categoryId
    ){
        PageBean<CService> pb = cServiceService.list(pageNum, pageSize, categoryId);
        return Result.success(pb);
    }

    @PostMapping("/update")
    public Result update(@RequestBody @Validated String id){
        if(!Authentication()){
            return Result.error("权限不足");
        }
        cServiceService.update(id);
        return Result.success();
    }

}
